package com.gopay.bis.manager.bank.bcdl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.dao.corp.CustCorpInfoDao;
import com.gopay.bis.manager.bank.NuccSerialManager;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BankOfAffiliationConstants;
import com.gopay.common.cps.manager.order.CpsGeneralMainOrderQueryManager;
import com.gopay.common.domain.bank.BcdlMessage;
import com.gopay.common.domain.bank.nucc.bcdl.eft.req.NuccBcdlBodyPyeeAcctInf;
import com.gopay.common.domain.bank.nucc.bcdl.eft.req.NuccBcdlBodyPyerAcctInf;
import com.gopay.common.domain.bank.nucc.bcdl.eft.req.NuccBcdlBodyPyerInf;
import com.gopay.common.domain.bank.nucc.bcdl.eft.req.NuccBcdlBodyReq;
import com.gopay.common.domain.bank.nucc.bcdl.eft.req.NuccBcdlBodyTrxInf;
import com.gopay.common.domain.bank.nucc.common.base.NuccBaseHeaderMessage;
import com.gopay.common.domain.bank.nucc.common.base.NuccBaseMessage;
import com.gopay.common.domain.bank.nucc.common.query.req.NuccQueryBodyOriTrxinf;
import com.gopay.common.domain.bank.nucc.common.query.req.NuccQueryBodyReq;
import com.gopay.common.domain.bank.nucc.common.query.res.NuccQueryRes;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.cust.CustCorpInfo;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.businesses.BcdlService;
import com.gopay.remote.bank.businesses.NuccCommonService;

/**
 * 网联付款申请拼装报文对象
 * 
 * @NuccBcdlManager.java
 * @author zhengxue
 * @2018-1-16 下午4:07:18 www.gopay.com.cn Inc.All rights reserved.
 */
public class NuccBcdlManager {
    @Resource(name = "nuccConfig")
    private Map<String, String> nuccConfig;

    @Resource(name = "nuccBcdlService")
    private BcdlService nuccBcdlService;

    @Resource(name = "cpsGeneralMainOrderQueryManager")
    private CpsGeneralMainOrderQueryManager mainOrderQuery;

    @Resource(name = "custCorpInfoDao")
    private CustCorpInfoDao custCorpInfoDao;

    @Resource(name = "nuccUrlConfig")
    private Map<String, String> nuccUrlConfig;

    @Resource(name = "nuccCommonServiceClient")
    private NuccCommonService nuccCommonServiceClient;

    @Resource(name = "nuccSerialManager")
    private NuccSerialManager nuccSerialManager;

    public Object eft(BcdlMessage msg) throws GopayException {
        NuccBaseMessage msgSnd = buildMessage(msg);
        BankLogUtils.printNuccPayManagerSendLog(msgSnd);
        Object o = nuccBcdlService.eft(msgSnd);
        BankLogUtils.printNuccPayManagerReturnLog(o);
        return o;
    }

    public Object query(BcdlMessage msg) throws GopayException {
        NuccBaseMessage message = buildQueryOrder(msg);
        BankLogUtils.printNuccPayManagerSendLog(message);
        NuccQueryRes res = (NuccQueryRes) nuccCommonServiceClient.queryTransStatus(message);
        BankLogUtils.printNuccPayManagerReturnLog(res);
        return res;
    }

    private NuccBaseMessage buildMessage(BcdlMessage msg) {
        NuccBaseMessage msgSnd = new NuccBaseMessage();
        BeanUtils.copyProperties(msg, msgSnd);
        NuccBaseHeaderMessage header = new NuccBaseHeaderMessage();
        NuccBcdlBodyReq body = new NuccBcdlBodyReq();

        header.setSndDt(DateUtils.now2string("yyyy-MM-dd HH:mm:ss").replace(" ", "T"));
        // 报文类型代码
        header.setMsgTp(nuccConfig.get("NUCC.BCDL.PAY.MSGTP"));
        // 发起方所属机构标示
        header.setIssrId(nuccConfig.get("GOPAY.ISSRID"));
        // 报文方向 11-支付机构到平台
        header.setDrctn(nuccConfig.get("NUCC.DRCTN.TONUCC"));
        // 签名证书序列号
        header.setSignSN(nuccConfig.get("NUCC.BCDL.SIGNSN"));
        // 加密证书序列号
        header.setNcrptnSN(nuccConfig.get("NUCC.EXPAY.NCRPTNSN"));

        NuccBcdlBodyPyerAcctInf pyerAcctInf = new NuccBcdlBodyPyerAcctInf();
        // 付款行金融机构编码
        pyerAcctInf.setPyerBkId(nuccConfig.get("NUCC.BCDL.GOPAY.RESFD.ACCT.ISSRID"));
        // 备付金账户编号
        pyerAcctInf.setPyerBkNo(nuccConfig.get("NUCC.BCDL.GOPAY.INSTG.ACCT.ID"));
        // 备付金账户名称
        pyerAcctInf.setPyerBkNm(nuccConfig.get("NUCC.BCDL.GOPAY.INSTG.ACCT.NM"));

        NuccBcdlBodyPyerInf pyerInf = new NuccBcdlBodyPyerInf();
        // 支付机构金融机构编码
        pyerInf.setPyerAcctIssrId(nuccConfig.get("GOPAY.ISSRID"));
        // 付款方账户类型
        pyerInf.setPyerAcctTp("04"); // 单位支付账户
        CpsGenMainOrder mainOrder = mainOrderQuery.get(msg.getOrderId());
        // 获取商户信息
        CustCorpInfo custCorpInfo = custCorpInfoDao.getByCustId(mainOrder.getPayCustId());
        // 付款方账户编号
        pyerInf.setPyerAcctNo(mainOrder.getPayAcct());
        // 付款方账户名称
        pyerInf.setPyerAcctNm(custCorpInfo.getCorpName());
        // 付款商户编号
//        pyerInf.setPyerMrchntNo(mainOrder.getPayCustId());
//        // 付款商户名称
//        pyerInf.setPyerMrchntNm(custCorpInfo.getMerName());
//        // 付款商户简称
//        pyerInf.setPyerMrchntShrtNm(custCorpInfo.getCorpShortName());

        NuccBcdlBodyPyeeAcctInf pyeeAcctInf = new NuccBcdlBodyPyeeAcctInf();
        if ("1".equals(msg.getDcblType() + "")) {
            // 对公
            pyeeAcctInf.setPyeeAcctTp("05"); // 对公银行账户
        } else if ("2".equals(msg.getDcblType() + "")) {
            // 对私
//            pyeeAcctInf.setPyeeAcctTp("00");// 个人银行借记账户
            pyeeAcctInf.setPyeeAcctTp("01");
        }

        /**String bankName = msg.getPbocBankName();
        String bank = "";
        if (bankName.indexOf("工行") != -1 || bankName.indexOf("工商") != -1) {
            bank = BankConstants.BANK_LOGO_ICBC;
        } else if (bankName.indexOf("招行") != -1 || bankName.indexOf("招商") != -1) {
            bank = BankConstants.BANK_LOGO_CMB;
        } else if (bankName.indexOf("农行") != -1 || bankName.indexOf("农业银行") != -1) {
            bank = BankConstants.BANK_LOGO_ABC;
        } else if (bankName.indexOf("中行") != -1 || bankName.indexOf("中国银行") != -1) {
            bank = BankConstants.BANK_LOGO_BOC;
        } else if (bankName.indexOf("建行") != -1 || bankName.indexOf("建设银行") != -1) {
            bank = BankConstants.BANK_LOGO_CCB;
        }**/
        // 收款行金融机构编码
        pyeeAcctInf.setPyeeBkId(BankOfAffiliationConstants.getAffiliation(msg.getBank()));
        // 收款方银行账户编号
        pyeeAcctInf.setPyeeBkNo(msg.getRecvBankAcctNum());
        // 收款方银行账户名称
        pyeeAcctInf.setPyeeBkNm(msg.getRecvBankAcctName());

        NuccBcdlBodyTrxInf trxInf = new NuccBcdlBodyTrxInf();
        // 付款
        trxInf.setTrxCtgy(nuccConfig.get("NUCC.BCDL.PAY.TRXCTGY"));
        // 业务种类
        trxInf.setBizTp("140099"); // 其他商户结算
        //
        trxInf.setTrxId(msg.getOutOrderId());
        trxInf.setTrxDtTm(DateUtils.now2string("yyyy-MM-dd HH:mm:ss").replace(" ", "T"));
        trxInf.setTrxAmt("CNY" + msg.getAmount().toString());
        trxInf.setTrxTrmTp("01");
        // trxInf.setTrxTrmNo("");

        body.setPyerAcctInf(pyerAcctInf);
        body.setPyerInf(pyerInf);
        body.setPyeeAcctInf(pyeeAcctInf);
        body.setTrxInf(trxInf);
        msgSnd.setMsgHeader(header);
        msgSnd.setMsgBody(body);
        // 设置支付地址
        String bankOrderId = msg.getOutOrderId();
        String ip = bankOrderId.substring(bankOrderId.length() - 6, bankOrderId.length() - 4);
        msgSnd.setPayUrl(nuccSerialManager.getUrlBySerial(ip));
        return msgSnd;
    }

    public NuccBaseMessage buildQueryOrder(BcdlMessage base) {
        NuccBaseMessage message = new NuccBaseMessage();
        NuccBaseHeaderMessage header = new NuccBaseHeaderMessage();
        NuccQueryBodyReq body = new NuccQueryBodyReq();

        header.setSndDt(DateUtils.now2string("yyyy-MM-dd HH:mm:ss").replace(" ", "T"));
        // 报文类型代码
        header.setMsgTp(nuccConfig.get("NUCC.QUERY.MSGTP"));
        // 发起方所属机构标示
        header.setIssrId(nuccConfig.get("GOPAY.ISSRID"));
        // 报文方向 11-支付机构到平台
        header.setDrctn(nuccConfig.get("NUCC.DRCTN.TONUCC"));
        // 签名证书序列号
        header.setSignSN(nuccConfig.get("NUCC.QUERY.SIGNSN"));

        NuccQueryBodyOriTrxinf oriTrxInf = new NuccQueryBodyOriTrxinf();
        // 支付交易类别
        oriTrxInf.setOriTrxCtgy(nuccConfig.get("NUCC.BCDL.PAY.TRXCTGY"));
        oriTrxInf.setOriTrxId(base.getOutOrderId());
        oriTrxInf.setOriTrxDtTm(base.getOrderDate().replace(" ", "T"));
        body.setOriTrxInf(oriTrxInf);
        body.setInstgId(nuccConfig.get("GOPAY.ISSRID"));
        // 设置支付地址
        String bankOrderId = base.getOutOrderId();
        String ip = bankOrderId.substring(bankOrderId.length() - 6, bankOrderId.length() - 4);
        message.setPayUrl(nuccSerialManager.getUrlBySerial(ip));
        message.setMsgHeader(header);
        message.setMsgBody(body);
        return message;
    }
    
    public Object qryAcctDetail(Object obj) throws GopayException {
        BcdlMessage msg = (BcdlMessage) obj;
        Map map = new HashMap<String, Object>();
        map =  (Map)nuccBcdlService.queryAcctDetail(msg);
        return map;
    }
}
